<?php

class Licenses extends MY_SecuredController {

	function Licenses() {
		parent::MY_SecuredController();
		
		$this->requireRole('Owner');

		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');
	}

	function index() {
		$this->_preRender(Current_User::user()->id);
		
		$data['licenses'] = Doctrine::getTable('LicenseModel')->findAll();
		$data['unlicensed'] = PilotLicenseModel::getUnlicensedCount();
		$licensed = PilotLicenseModel::getLicensedByTypeCount();
		
		$data['unlicensed_count'] = $data['unlicensed']['count'];
		$count = 0;
		
		foreach ($licensed as $key => $value) {
			$count += ($value['pilots'] + 0);
			$data['licensed'][$value['id']] = $value['pilots'];
		}
        
		$data['licensed_count'] = $count;
		
		$this->render('admin/licenses/index', $data);
	}

	function see($id) {
        $this->_preRender(Current_User::user()->id);
        
        $data['license'] = LicenseModel::getLicense($id);
        $data['selected'] = $id;
        
        if (!$data['license']) {
            $this->redirect('admin/licenses/index', array(
                    'message' => 'Ooops, that license could not be found - choose another one.',
            ), true);           
        }
        
        $data['licenses'] = Doctrine::getTable('LicenseModel')->findAll();
        
        $this->render('admin/licenses/view', $data);
    }
	
	function add() {
        $this->_preRender(Current_User::user()->id);
        
        if (!$this->input->post('action')) {
    
            $data['license'] = new LicenseModel();
            $this->render('admin/licenses/add', $data);
        }
        else {
            if ($this->_submit_validate() === FALSE) {
                $data['license'] = new LicenseModel();
                $this->render('admin/licenses/add', $data, $this->error(validation_errors('<li>','</li>')));
                return;
            }
    
            $c = new LicenseModel();
            $c->merge($_POST);
    
            try {
                $c->save();
            }
            catch (Exception $e) {
                $this->render('admin/licenses/index', $data, $this->error(array(
                    'message' => 'Ooops, the license could not be added - please try again later.',
                    'exception' => $e
                )));
                return;
            }
    
            $this->redirect('admin/licenses/index','License added successfully!');
        }
    }

    function edit($id) {
        $this->_preRender(Current_User::user()->id);
        $data['licenses'] = Doctrine::getTable('LicenseModel')->findAll();
         
        if (!$this->input->post('action')) {
            $this->_preRender(Current_User::user()->id);

            $data['license'] = LicenseModel::getLicense($id);
             
            $this->render('admin/licenses/edit', $data);
        }
        else {
            $license = LicenseModel::getLicense($id);

            if (!$license) {
                $this->render('admin/licenses/index',$data, $this->error(array(
                    'message' => 'Ooops, the license could not be found, operation has been cancelled!',
                )));
                return;
            }

            $license->merge($_POST);

            try {
                $license->save();
            }
            catch (Exception $e) {
                $this->render('admin/licenses/index', $data, $this->error(array(
                    'message' => 'Ooops, the information could not be updated, please try again later.',
                    'exception' => $e
                )));
                return;
            }

            $this->redirect('admin/licenses/index', 'License has been successfully updated!');
        }
    }

    function delete($id) {
        $this->_preRender(Current_User::user()->id);
        $data['licenses'] = Doctrine::getTable('LicenseModel')->findAll();

        $license = LicenseModel::getLicense($id);

        if (!$license) {
            $this->redirect('admin/licenses/index', array(
                    'message' => 'Ooops, the license could not be found, operation has been cancelled!',
            ));
            return;
        }

        try {
            $license->delete();
        }
        catch (Exception $e) {
            $this->redirect('admin/licenses/index', array(
                    'message' => 'Ooops, the license could not be deleted, please try again later.',
                    'exception' => $e
            ), true);
            
            return;
        }

        $this->redirect('admin/licenses/index', 'License has been successfully deleted!');
    }
    
	private function _submit_validate() {
		$this->form_validation->set_rules('issuer', 'Issuer',
			'required|alpha_numeric[License.issuer]');

		return $this->form_validation->run();
	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */